<?php

/**
 * @author Rizky Tahara Shita
 * @website http://tahara.web.id
 * @email rizky.tahara@gmail.com
 * @copyright b'IT Cyber Solution
 */

require_once(APPPATH.'controllers/admin/applications.php');
class Profile extends Applications
{
	public function __construct()
	{
		parent::__construct();

		/**
		 * @description check the session variables
		 */
		$this->M_login->session_check();

		/**
		 * @description the URL
		 */
		$this->bz_segment['END'] = 5;
		$this->bz_segment['URL'] = $this->M_functions->segment_create();
		$this->bz_segment['SITE_URL'] = site_url($this->bz_segment['URL']);

		/**
		 * @description access rights
		 */
		$this->access_rights = $this->M_menu->valid_check('db_default', $this->session->userdata('session_user_id'));
		$this->access_rights = array
			(
				'ADD' => in_array('add', $this->access_rights) ? TRUE : FALSE
				, 'EDIT' => in_array('edit', $this->access_rights) ? TRUE : FALSE
				, 'DELETE' => in_array('delete', $this->access_rights) ? TRUE : FALSE
			);

		/**
		 * @description initialize variables for database
		 */
		$this->tbl_attr = array
			(
				'TABLE' => array
					(
						'NAME' => 'hris_organization_company'
						, 'NAME_ALIAS' => 'hris_organization_company a'
						, 'PRIMARY' => array
							(
								'SHOW' => FALSE
								, 'DATA' => array('id')
							)
					)
				, 'VIEW' => array
					(
						'HEADING' => array
							(
								$this->lang->line('hris_code')
								, $this->lang->line('hris_name')
								, $this->lang->line('hris_type')
								, $this->lang->line('hris_company_level')
								, $this->lang->line('hris_company_owner_name')
								, $this->lang->line('hris_phone')
								, $this->lang->line('hris_fax')
								, $this->lang->line('hris_email')
								, $this->access_rights['DELETE'] ? '&nbsp;' : NULL
							)
						, 'FIELDS' => 'id, code, name, type, (select name from bz_hris_master_data_language b where a.company_level__id = b.data__id and b.language__id = "'.$this->session->userdata('session_user_language_id').'") as company_level_name, owner_name, phone, fax, email'
						, 'FIELDS_EDIT' => 'name'
						, 'FIELDS_ORDER' => 'name'
						, 'FIELDS_PARENT_NAME' => 'parent_id'
						, 'TABLE' => array
							(
								'WHERE' => array()
							)
					)
				, 'FORM' => array
					(
						/**
						 * @description selected the option in combobox which the datas is being called via ajax
						 * 					concat("ajax_call::", "'.$this->bz_segment['SITE_URL'].'", "::ajx_live_area_::combo_put::", country__id, "/46/13/::", province__id) as province_id'
						 * 					params: URL::controller::action::id_reference::modul_data::html_object_id::selected_value
						 */
						'FIELDS' => 'id, parent_id, code, type, name, name_official, company_level__id, owner_name, instantion_tax__id, tax_number, tax_ump_limit, address'
							. ', country__id'
							. ', concat("ajax_call::", "'.$this->bz_segment['SITE_URL'].'", "::ajx_live_area_::combo_put::", country__id, "/46/13/::", province__id) as province_id'
							. ', concat("ajax_call::", "'.$this->bz_segment['SITE_URL'].'", "::ajx_live_area_::combo_put::", province__id, "/47/14/::", city__id) as city_id'
							. ', zipcode, phone, fax, email, logo_attachment__id'
						, 'FIELDS_ORDER' => ''
						, 'COLUMN' => 2
					)
				, 'URL' => array
					(
						'FORM' => $this->bz_segment['SITE_URL'].'/form'
						, 'VIEW' => $this->bz_segment['SITE_URL'].'/view'
					)
			);

		/**
		 * @description template lite variables
		 */
		$this->bz_tlite_var['CACHE_PATH'] = $this->bz_tlite_var['SESSION']['USER_ID'].'|'.str_replace('/', '|', $this->bz_segment['URL']);
		$this->bz_tlite_var['CACHE'] = array
			(
				'PUBLIC' => $this->bz_tlite_var['SESSION']['USER_ID']
				, 'PRIVATE' => array
					(
						'GLOBAL' => $this->bz_tlite_var['CACHE_PATH']
						, 'VIEW' => $this->bz_tlite_var['CACHE_PATH'].'|view'
						, 'FORM' => $this->bz_tlite_var['CACHE_PATH'].'|form'
					)
			);
	}


	/**
	 * @function index()
	 * @description main controller
	 */
	public function index()
	{
		/**
		 * @description choose which controller to show
		 */
		redirect($this->tbl_attr['URL']['VIEW']);
	}


	/**
	 * @function view()
	 * @description view table list of data
	 */
	public function view()
	{
		if ($this->web_data['BISKITZ']['HEADER']['BROWSER_SUPPORTED'] == '')
		{
			/**
			 * @description load the models
			 */
			$this->load->model('M_viewlist');


			/**
			 * @description if there isn't any css/ajax cache, build it! else show cache!
			 */
			if ( ! $this->bz_tlite->is_cached($this->bz_tlite_var['NAMES']['CSS'], $this->bz_tlite_var['CACHE']['PRIVATE']['VIEW']))
			{
				/**
				 * @section begin page init
				 */
					/**
					 * @description show the menu
					 */
					$menu = $this->M_menu->shows();
					$this->web_data['JAVASCRIPT_MENU'] = $menu['JAVASCRIPT_MENU'];
					$this->web_data['PAGE']['MENU'] = $menu['PAGE']['MENU'];

					/**
					 * @description init the css and the ajax needed
					 */
					$css_files = array
						(
							array('layout' => 'screen')
							, array('tipsy/tipsy' => 'screen')
							, array('form_icon_bar' => 'screen')
							, array('nanoscroller/nanoscroller' => ($menu['PAGE']['MENU']['FAVORITE']['LIST'] != 'Error') ? 'screen' : NULL)
							, array('shortcut_bar' => ($menu['PAGE']['MENU']['FAVORITE']['LIST'] != 'Error') ? 'screen' : NULL)
							, array('ddm/multi-ddm' => 'screen')
						);
					$js_files = array
						(
							'js/plugins/ddm' => array('jquery.multi-ddm.pack')
							, 'js/plugins/nanoscroller' => ($menu['PAGE']['MENU']['FAVORITE']['LIST'] != 'Error') ? array('jquery.nanoscroller.min', 'overthrow.min') : NULL
							, 'js/plugins/tipsy' => array('jquery.tipsy.pack')
							, $this->web_data['BISKITZ']['SETTINGS']['BISKITZ_JS_FUNCTIONS_PATH'] => array('common', 'form', 'error')
							, 'themes/'.$this->web_data['BISKITZ']['SETTINGS']['BISKITZ_THEME_ADMIN'].'/'.$this->web_data['BISKITZ']['SETTINGS']['BISKITZ_JS_PATH'] => array('common')
						);
				/**
				 * @section end page init
				 */


				/**
				 * @section begin generate template variables
				 */
					/**
					 * @description css and ajax
					 */
					$this->web_data['PAGE']['CSS_FILES'] = $this->M_template->css_generate($css_files, $this->web_data['BISKITZ']['SETTINGS']['BISKITZ_THEME_ADMIN']);
					$this->web_data['PAGE']['JAVASCRIPT_FILES'] = $this->M_template->js_generate($js_files);

					/**
					 * @description initialize the additional javascript
					 */
					if ($menu['PAGE']['MENU']['FAVORITE']['LIST'] != 'Error')
					{
						$this->web_data['JAVASCRIPT'] .= '
							<script>
								$(function()
								{
									$(".nano").nanoScroller({preventPageScrolling: true});
								});
							</script>
							';
					}

					/**
					 * @description menu descriptions
					 */
					$this->web_data['PAGE']['DETAIL'] = $this->M_menu->detail();
				/**
				 * @section end generate template variables
				 */
			}


			/**
			 * @section begin table view init
			 */
				/**
				 * @description init the command button
				 */
				$this->load->model('admin/M_command_bar');
				$view_list_commands = $this->M_command_bar->view('', '', FALSE);

				/**
				 * @description init the table list
				 */
				$view_list_content = $this->M_viewlist->generate
					(
						'db_default'
						, $this->tbl_attr['VIEW']['FIELDS'], $this->tbl_attr['TABLE']['PRIMARY'], $this->tbl_attr['VIEW']['FIELDS_EDIT'], $this->tbl_attr['VIEW']['FIELDS_ORDER']
						, $this->tbl_attr['TABLE']['NAME_ALIAS'], $this->tbl_attr['VIEW']['TABLE']['WHERE']
						, ''
						, $this->bz_segment['URL']
						, 0
						, 'form'
						, $this->access_rights['EDIT']
						, $this->access_rights['DELETE']
						, 'none'
						, FALSE
						, FALSE, ''
						, TRUE
						, $this->tbl_attr['VIEW']['FIELDS_PARENT_NAME']
					);

				if (count($view_list_content))
				{
					$table_template = array('table_open' => '<table width = "100%">');
					$this->table->set_template($table_template);

					$this->table->set_heading($this->tbl_attr['VIEW']['HEADING']);
					$view_list_content = $this->table->generate($view_list_content);
				}
				else
				{
					$view_list_content = '<center class="error_field">'.$this->lang->line('data_view_error').'</center>';
				}
			/**
			 * @section end table view init
			 */


			/**
			 * @section begin generate template variables
			 */

				/**
				 * @description view list
				 */
				$this->web_data['PAGE']['VIEW'] = array
					(
						'FORM' => array
							(
								'OPEN' => form_open($this->tbl_attr['URL']['VIEW'], array('id' => 'frm_header', 'onSubmit' => 'return false'), '')
							)
						, 'COMMANDS' => $view_list_commands
						, 'LISTS' => $view_list_content
					);
			/**
			 * @section end generate template variables
			 */


			/**
			 * @description log the action!
			 */
			$this->M_log->write
				(
					'db_default'
					, sprintf($this->lang->line('log_view_access'), $this->router->class)
				);



		}
		else
		{
			/**
			 * @section Browser NOT Supported
			 */
				/**
				 * @description browser not supported message error
				 */
				$css_files = array(array('layout' => 'screen'));


				/**
				 * @section begin generate template variables
				 */
					/**
					 * @description css and ajax
					 */
					$this->web_data['PAGE']['CSS_FILES'] = $this->M_template->css_generate($css_files, $this->web_data['BISKITZ']['SETTINGS']['BISKITZ_THEME_ADMIN']);
				/**
				 * @section end generate template variables
				 */

		}



		/**
		 * @description output template variables
		 */
		$this->bz_tlite->assign('WEB_DATA', $this->web_data);


		/**
		 * @section begin output
		 */
		$this->bz_tlite->display($this->bz_tlite_var['NAMES']['HEADER'], $this->bz_tlite_var['CACHE']['PUBLIC']);
		$this->bz_tlite->display($this->bz_tlite_var['NAMES']['CSS'], $this->bz_tlite_var['CACHE']['PRIVATE']['VIEW']);
		$this->bz_tlite->display($this->bz_tlite_var['NAMES']['MENU'], $this->bz_tlite_var['CACHE']['PUBLIC']);
		$this->bz_tlite->display($this->bz_tlite_var['NAMES']['CONTENT_DESCRIPTION'], $this->bz_tlite_var['CACHE']['PRIVATE']['GLOBAL']);
		$this->bz_tlite->display($this->bz_tlite_var['NAMES']['CONTENT_FORM_COMMAND_BAR'], $this->bz_tlite_var['CACHE']['PRIVATE']['VIEW']);

		$this->bz_tlite->cache = FALSE;
		$this->bz_tlite->display($this->bz_tlite_var['NAMES']['CONTENT_FORM']);

		$this->bz_tlite->cache = TRUE;
		$this->bz_tlite->display($this->bz_tlite_var['NAMES']['CONTENT_SHORTCUT_BAR'], $this->bz_tlite_var['CACHE']['PUBLIC']);
		$this->bz_tlite->display($this->bz_tlite_var['NAMES']['FOOTER'], $this->bz_tlite_var['CACHE']['PUBLIC']);
		$this->bz_tlite->display($this->bz_tlite_var['NAMES']['JAVASCRIPT'], $this->bz_tlite_var['CACHE']['PRIVATE']['VIEW']);
		$this->bz_tlite->display($this->bz_tlite_var['NAMES']['FOOTER_END'], $this->bz_tlite_var['CACHE']['PUBLIC']);
		/**
		 * @section end output
		 */
	}


	/**
	 * @function form()
	 * @description form for data entry
	 */
	public function form($prm_key = '')
	{
		if ($this->web_data['BISKITZ']['HEADER']['BROWSER_SUPPORTED'] == '')
		{
			/**
			 * @description if the user has ADD or EDIT access, show the form!
			 */
			if ($this->access_rights['ADD'] OR ($this->access_rights['EDIT'] && trim($prm_key) != ''))
			{
				/**
				 * @description if there isn't any css/ajax cache, build it! else show cache!
				 */
				if ( ! $this->bz_tlite->is_cached($this->bz_tlite_var['NAMES']['CSS'], $this->bz_tlite_var['CACHE']['PRIVATE']['FORM']))
				{
					/**
					 * @section begin page init
					 */
						/**
						 * @description show the menu
						 */
						$menu = $this->M_menu->shows();
						$this->web_data['JAVASCRIPT_MENU'] = $menu['JAVASCRIPT_MENU'];
						$this->web_data['PAGE']['MENU'] = $menu['PAGE']['MENU'];

						/**
						 * @description init the css and the ajax needed
						 */
						$css_files = array
							(
								array('layout' => 'screen')
								, array('tipsy/tipsy' => 'screen')
								, array('validate/validate' => 'screen')
								, array('form_icon_bar' => 'screen')
								, array('nanoscroller/nanoscroller' => ($menu['PAGE']['MENU']['FAVORITE']['LIST'] != 'Error') ? 'screen' : NULL)
								, array('shortcut_bar' => ($menu['PAGE']['MENU']['FAVORITE']['LIST'] != 'Error') ? 'screen' : NULL)
								, array('ddm/multi-ddm' => 'screen')
							);
						$js_files = array
							(
								'js/plugins/ddm' => array('jquery.multi-ddm.pack')
								, 'js/plugins/nanoscroller' => ($menu['PAGE']['MENU']['FAVORITE']['LIST'] != 'Error') ? array('jquery.nanoscroller.min', 'overthrow.min') : NULL
								, 'js/plugins/validate' => array('jquery.validate.pack', 'jquery.metadata')
								, 'js/plugins/tipsy' => array('jquery.tipsy.pack')
								, 'js/plugins/fileupload' => array('ajaxfileupload')
								, $this->web_data['BISKITZ']['SETTINGS']['BISKITZ_JS_FUNCTIONS_PATH'] => array('common', 'form', 'error')
								, 'themes/'.$this->web_data['BISKITZ']['SETTINGS']['BISKITZ_THEME_ADMIN'].'/'.$this->web_data['BISKITZ']['SETTINGS']['BISKITZ_JS_PATH'] => array('common')
							);


						/**
						 * @section begin form initialize
						 */
							/**
							 * @description load the models
							 */
							$this->load->model('admin/hris/HRIS_M_live_data');


							/**
							 * @description initialize the form elements
							 */
							$form_content = array
								(
									'<b>'.$this->lang->line('hris_parent').'</b>'
									, form_input
										(
											array
											(
												'type' => 'hidden'
												, 'name' => 'txtinput[0]', 'id' => 'txt0'
												, 'size' => '7', 'maxlength' => '7'
												, 'readOnly' => 'readOnly', 'class' => 'read_only'
											)
										)
										. form_dropdown
											(
												'txtinput[1]'
												, $this->HRIS_M_live_data->combo_fill('db_default', 'company_profile', '', 'RECURSIVE')
												, ''
												, 'id="txt1" class="{validate: {required: true}}"'
													. ' shortcut_f2="'.$this->tbl_attr['URL']['VIEW'].'"'
											)

									, '<b>'.$this->lang->line('hris_code').'</b><br /><small>'.$this->lang->line('fields_required').'</small>'
									, form_input
										(
											array
											(
												'name' => 'txtinput[2]', 'id' => 'txt2'
												, 'size' => '10', 'maxlength' => '10'
												, 'class' => '{validate: {required: true}}'
												, 'shortcut_f2' => $this->tbl_attr['URL']['VIEW']
											)
										)

									, '<b>'.$this->lang->line('hris_type').'</b>'
									, form_input
										(
											array
											(
												'name' => 'txtinput[3]', 'id' => 'txt3'
												, 'size' => '25', 'maxlength' => '25'
												, 'shortcut_f2' => $this->tbl_attr['URL']['VIEW']
											)
										)

									, '<b>'.$this->lang->line('hris_name').'</b><br /><small>'.$this->lang->line('fields_required').'</small>'
									, form_input
										(
											array
											(
												'name' => 'txtinput[4]', 'id' => 'txt4'
												, 'size' => '40', 'maxlength' => '40'
												, 'class' => '{validate: {required: true}}'
												, 'shortcut_f2' => $this->tbl_attr['URL']['VIEW']
											)
										)

									, '<b>'.$this->lang->line('hris_company_name_official').'</b>'
									, form_input
										(
											array
											(
												'name' => 'txtinput[5]', 'id' => 'txt5'
												, 'size' => '50', 'maxlength' => '50'
												, 'shortcut_f2' => $this->tbl_attr['URL']['VIEW']
											)
										)

									, '<b>'.$this->lang->line('hris_company_level').'</b>'
									, form_dropdown
										(
											'txtinput[6]'
											, $this->HRIS_M_live_data->combo_fill('db_default', 17)
											, ''
											, 'id="txt6" class="{validate: {required: true}}"'
												. ' shortcut_f2="'.$this->tbl_attr['URL']['VIEW'].'"'
										)

									, '<b>'.$this->lang->line('hris_company_owner_name').'</b><br /><small>'.$this->lang->line('fields_required').'</small>'
									, form_input
										(
											array
											(
												'name' => 'txtinput[7]', 'id' => 'txt7'
												, 'size' => '60', 'maxlength' => '60'
												, 'class' => '{validate: {required: true}}'
												, 'shortcut_f2' => $this->tbl_attr['URL']['VIEW']
											)
										)

									, '<b>'.$this->lang->line('hris_instantion_tax').'</b>'
									, form_dropdown
										(
											'txtinput[8]'
											, $this->HRIS_M_live_data->combo_fill('db_default', 'hris_master_instantion', '', 'NORMAL', array('instantion_type__id' => 133))
											, ''
											, 'id="txt8" class="{validate: {required: true}}"'
												. ' shortcut_f2="'.$this->tbl_attr['URL']['VIEW'].'"'
										)

									, '<b>'.$this->lang->line('hris_company_tax_number').'</b>'
									, form_input
										(
											array
											(
												'name' => 'txtinput[9]', 'id' => 'txt9'
												, 'size' => '25', 'maxlength' => '25'
												, 'shortcut_f2' => $this->tbl_attr['URL']['VIEW']
											)
										)

									, '<b>'.$this->lang->line('hris_company_tax_ump_limit').'</b>'
									, form_input
										(
											array
											(
												'name' => 'txtinput[10]', 'id' => 'txt10'
												, 'size' => '15', 'maxlength' => '15'
												, 'class' => '{validate: {number: true, min: 0}} input_right'
												, 'shortcut_f2' => $this->tbl_attr['URL']['VIEW']
											)
											, '0'
										)

									, array('data' => '<b>'.$this->lang->line('hris_address').'</b>', 'valign' => 'top')
									, form_textarea
										(
											array
											(
												'name' => 'txtinput[11]', 'id' => 'txt11'
												, 'cols' => '60', 'rows' => '7'
											)
											, ''
											, 'shortcut_f2="'.$this->tbl_attr['URL']['VIEW'].'"'
										)

									, '<b>'.$this->lang->line('hris_area_country').'</b>'
									, form_dropdown
										(
											'txtinput[12]'
											, $this->HRIS_M_live_data->combo_fill('db_default', 'hris_master_area', '', 'NORMAL', array('parent_id' => 0))
											, ''
											, 'id="txt12" class="{validate: {required: true}}"'
												. ' shortcut_f2="'.$this->tbl_attr['URL']['VIEW'].'"'
										)

									, '<b>'.$this->lang->line('hris_area_province').'</b>'
									, form_dropdown
										(
											'txtinput[13]'
											, array('' => $this->lang->line('hris_choose'))
											, ''
											, 'id="txt13" class="{validate: {required: true}}"'
												. ' shortcut_f2="'.$this->tbl_attr['URL']['VIEW'].'"'
										)

									, '<b>'.$this->lang->line('hris_area_city').'</b>'
									, form_dropdown
										(
											'txtinput[14]'
											, array('' => $this->lang->line('hris_choose'))
											, ''
											, 'id="txt14" class="{validate: {required: true}}"'
												. ' shortcut_f2="'.$this->tbl_attr['URL']['VIEW'].'"'
										)

									, '<b>'.$this->lang->line('hris_zipcode').'</b>'
									, form_input
										(
											array
											(
												'name' => 'txtinput[15]', 'id' => 'txt15'
												, 'size' => '5', 'maxlength' => '5'
												, 'shortcut_f2' => $this->tbl_attr['URL']['VIEW']
											)
										)

									, '<b>'.$this->lang->line('hris_phone').'</b>'
									, form_input
										(
											array
											(
												'name' => 'txtinput[16]', 'id' => 'txt16'
												, 'size' => '20', 'maxlength' => '20'
												, 'shortcut_f2' => $this->tbl_attr['URL']['VIEW']
											)
										)

									, '<b>'.$this->lang->line('hris_fax').'</b>'
									, form_input
										(
											array
											(
												'name' => 'txtinput[17]', 'id' => 'txt17'
												, 'size' => '20', 'maxlength' => '20'
												, 'shortcut_f2' => $this->tbl_attr['URL']['VIEW']
											)
										)

									, '<b>'.$this->lang->line('hris_email').'</b>'
									, form_input
										(
											array
											(
												'name' => 'txtinput[18]', 'id' => 'txt18'
												, 'size' => '60', 'maxlength' => '60'
												, 'class' => '{validate: {email: true}}'
												, 'shortcut_f2' => $this->tbl_attr['URL']['VIEW']
											)
										)

									, '<b>'.$this->lang->line('hris_company_logo').'</b>'
									, form_input
										(
											array
											(
												'type' => 'hidden'
												, 'name' => 'txtinput[19]', 'id' => 'txt19'
												, 'readOnly' => 'readOnly', 'class' => 'read_only'
											)
										)
										. form_input
											(
												array
												(
													'type' => 'hidden'
													, 'name' => 'txtinput[20]', 'id' => 'txt20'
													, 'readOnly' => 'readOnly', 'class' => 'read_only'
												)
											)
										. form_input
											(
												array
												(
													'type' => 'hidden'
													, 'name' => 'txtinput[21]', 'id' => 'txt21'
													, 'readOnly' => 'readOnly', 'class' => 'read_only'
												)
											)
										. form_upload
											(
												array
												(
													'name' => 'txtupload_file', 'id' => 'txt500'
													, 'size' => '30'
													, 'class' => '{validate: {accept:\''.$this->web_data['BISKITZ']['SETTINGS']['BISKITZ_UPLOAD_TYPE_ALLOWED'].'\'}}'
												)
											)


										. form_submit
											(
												array('id' => 'btn_save', 'class' => 'hidden')
												, $this->lang->line('button_save')
											)
								);

							/**
							 * @description create the form and elements
							 */
							$form_content = $this->table->make_columns($form_content, $this->tbl_attr['FORM']['COLUMN']);
							$form_content = $this->table->generate($form_content);
						/**
						 * @section end form initialize
						 */
					/**
					 * @section end page init
					 */



					/**
					 * @section begin generate template variables
					 */
						/**
						 * @description css and ajax
						 */
						$this->web_data['PAGE']['CSS_FILES'] = $this->M_template->css_generate($css_files, $this->web_data['BISKITZ']['SETTINGS']['BISKITZ_THEME_ADMIN']);
						$this->web_data['PAGE']['JAVASCRIPT_FILES'] = $this->M_template->js_generate($js_files);

						/**
						 * @description initialize the additional javascript
						 */
						$this->web_data['JAVASCRIPT'] .= '
							<script>
								$(function()
								{
									$("#btn_save").click
										(function()
										{
											js_form_action_validate
											(
												"'.$this->bz_segment['SITE_URL'].'", "ajx_", "insert", ""
												, "#frm_header", "#process", "#error_message"
												, "validate"
												, "TRUE"
												, "'.$this->bz_segment['SITE_URL'].'", "ajx_media_", "upload_file", "txt500"
											);
										});

									$("#txt12").change
										(function()
										{
											$("#txt13, #txt14").empty()
												.append( $("<option></option>").val("").html("'.$this->lang->line('hris_choose').'") );

											if ($(this).val() != "")
											{
												js_form_action
												(
													"'.$this->bz_segment['SITE_URL'].'", "ajx_live_area_", "combo_put", $(this).val() + "/46/13"
													, "#frm_header", "#process", "#error_message"
												);
											}
										});

									$("#txt13").change
										(function()
										{
											$("#txt14").empty()
												.append( $("<option></option>").val("").html("'.$this->lang->line('hris_choose').'") );

											if ($(this).val() != "")
											{
												js_form_action
												(
													"'.$this->bz_segment['SITE_URL'].'", "ajx_live_area_", "combo_put", $(this).val() + "/47/14"
													, "#frm_header", "#process", "#error_message"
												);
											}
										});

									$("#txt1").focus().select();
						';

						if ($menu['PAGE']['MENU']['FAVORITE']['LIST'] != 'Error')
						{
							$this->web_data['JAVASCRIPT'] .= '$(".nano").nanoScroller({preventPageScrolling: true});';
						}

						$this->web_data['JAVASCRIPT'] .= '
								});
							</script>
						';

						/**
						 * @description menu descriptions
						 */
						$this->web_data['PAGE']['DETAIL'] = $this->M_menu->detail();

						/**
						 * @description icon bar
						 */
						$this->load->model('admin/M_command_bar');
						$this->web_data['PAGE']['ICON_BAR'] = $this->M_command_bar->form();

						/**
						 * @description form dialogs
						 */
						$this->web_data['PAGE']['FORM'] = array
							(
								'OPEN' => form_open('', array('id' => 'frm_header', 'onSubmit' => 'return false'), '')
								, 'CONTENT' => $form_content
								, 'CENTER' => 'FALSE'
							);

						/**
						 * @description notes
						 */
						$this->web_data['PAGE']['NOTES'] = array
							(
								'USE' => 'TRUE'
								, 'MESSAGE' => array
									(
										'SHORTCUTS' => $this->lang->line('notes_shortcuts')
										, 'ADDITIONAL' => $this->lang->line('notes_additional')
									)
							);
					/**
					 * @section end generate template variables
					 */
				}


				/**
				 * @description get the data for editing via ajax
				 */
				if ($this->access_rights['EDIT'] && trim($prm_key) != '')
				{
					$this->web_data['JAVASCRIPT_EXTEND'] = '
						<script>
							$(function()
							{
								js_form_action
								(
									"'.$this->bz_segment['SITE_URL'].'", "ajx_data_", "put", "'.$prm_key.'"
									, "#frm_header", "#process", "#error_message"
								);
							});
						</script>
					';
				}


				/**
				 * @description log the action!
				 */
				$this->M_log->write('db_default', sprintf($this->lang->line('log_form_access'), $this->router->class, 'form'));
			}
			else
			{
				/**
				 * @description doesn't have ADD or EDIT access? redirect to desktop
				 */
				redirect('admin/desktop');
			}



		}
		else
		{
			/**
			 * @section Browser NOT Supported
			 */
				/**
				 * @description browser not supported message error
				 */
				$css_files = array(array('layout' => 'screen'));


				/**
				 * @section begin generate template variables
				 */
					/**
					 * @description css and ajax
					 */
					$this->web_data['PAGE']['CSS_FILES'] = $this->M_template->css_generate($css_files, $this->web_data['BISKITZ']['SETTINGS']['BISKITZ_THEME_ADMIN']);
				/**
				 * @section end generate template variables
				 */
		}



		/**
		 * @description output template variables
		 */
		$this->bz_tlite->assign('WEB_DATA', $this->web_data);


		/**
		 * @section begin output
		 */
		$this->bz_tlite->display($this->bz_tlite_var['NAMES']['HEADER'], $this->bz_tlite_var['CACHE']['PUBLIC']);
		$this->bz_tlite->display($this->bz_tlite_var['NAMES']['CSS'], $this->bz_tlite_var['CACHE']['PRIVATE']['FORM']);
		$this->bz_tlite->display($this->bz_tlite_var['NAMES']['MENU'], $this->bz_tlite_var['CACHE']['PUBLIC']);
		$this->bz_tlite->display($this->bz_tlite_var['NAMES']['CONTENT_DESCRIPTION'], $this->bz_tlite_var['CACHE']['PRIVATE']['GLOBAL']);
		$this->bz_tlite->display($this->bz_tlite_var['NAMES']['CONTENT_FORM_ICON_BAR'], $this->bz_tlite_var['CACHE']['PRIVATE']['FORM']);
		$this->bz_tlite->display($this->bz_tlite_var['NAMES']['CONTENT_FORM'], $this->bz_tlite_var['CACHE']['PRIVATE']['FORM']);
		$this->bz_tlite->display($this->bz_tlite_var['NAMES']['CONTENT_SHORTCUT_BAR'], $this->bz_tlite_var['CACHE']['PUBLIC']);
		$this->bz_tlite->display($this->bz_tlite_var['NAMES']['FOOTER'], $this->bz_tlite_var['CACHE']['PUBLIC']);
		$this->bz_tlite->display($this->bz_tlite_var['NAMES']['JAVASCRIPT'], $this->bz_tlite_var['CACHE']['PRIVATE']['FORM']);

		$this->bz_tlite->cache = FALSE;
		$this->bz_tlite->display($this->bz_tlite_var['NAMES']['JAVASCRIPT_EXTEND']);

		$this->bz_tlite->cache = TRUE;
		$this->bz_tlite->display($this->bz_tlite_var['NAMES']['FOOTER_END'], $this->bz_tlite_var['CACHE']['PUBLIC']);
		/**
		 * @section end output
		 */
	}



/**
 * @section AJAX Action Section
 */

	/**
	 * @function ajx_insert()
	 * @description insert the data
	 */
	public function ajx_insert()
	{
		if ($this->input->is_ajax_request() && ($this->access_rights['ADD'] OR $this->access_rights['EDIT']))
		{
			if ($this->input->post('txtinput[0]') != $this->input->post('txtinput[1]'))
			{
				/**
				 * @description load the models
				 */
				$this->load->model('admin/M_live_data');


				/**
				 * @description user choose logo to upload? add to master_media and get the attachment__id
				 */
				$logo_attachment_id = '';
				if (trim($this->input->post('txtinput[20]') != ''))
				{
					$file_data = array
						(
							'date' => date('Y-m-d H:i:s')
							, 'title' => $this->input->post('txtinput[4]').' Logo'
							, 'file_name' => $this->input->post('txtinput[20]')
							, 'file_mime' => $this->input->post('txtinput[21]')
							, 'slug' => 'company-logo-of-'.url_title($this->input->post('txtinput[4]'), 'dash', TRUE)
						);
					$this->M_live_data->insert
						(
							'db_default'
							, 'hris_master_media'
							, array('id' => NULL)
							, $file_data
							, 'form'
							, FALSE, ''
							, FALSE
						);

					$logo_attachment_id = $this->M_database->max_id('db_default', 'hris_master_media', 'id');
				}


				$company_profile_data = array
					(
						'parent_id' => $this->input->post('txtinput[1]')
						, 'code' => $this->input->post('txtinput[2]')
						, 'type' => $this->input->post('txtinput[3]')
						, 'name' => $this->input->post('txtinput[4]')
						, 'name_official' => $this->input->post('txtinput[5]')
						, 'company_level__id' => $this->input->post('txtinput[6]')
						, 'owner_name' => $this->input->post('txtinput[7]')
						, 'instantion_tax__id' => $this->input->post('txtinput[8]')
						, 'tax_number' => $this->input->post('txtinput[9]')
						, 'tax_ump_limit' => $this->input->post('txtinput[10]')
						, 'address' => $this->input->post('txtinput[11]')
						, 'country__id' => $this->input->post('txtinput[12]')
						, 'province__id' => $this->input->post('txtinput[13]')
						, 'city__id' => $this->input->post('txtinput[14]')
						, 'zipcode' => $this->input->post('txtinput[15]')
						, 'phone' => $this->input->post('txtinput[16]')
						, 'fax' => $this->input->post('txtinput[17]')
						, 'email' => $this->input->post('txtinput[18]')
					);
				$return_value = $this->M_live_data->insert
					(
						'db_default'
						, $this->tbl_attr['TABLE']['NAME']
						, array('id' => $this->input->post('txtinput[0]'))
						, $logo_attachment_id != ''
							? array_merge
								(
									array('logo_attachment__id' => $logo_attachment_id)
									, $company_profile_data
								)
							: $company_profile_data
						, 'form'
						, FALSE, $this->tbl_attr['URL']['VIEW']
						, FALSE
					);

				if ($return_value[0] == 'js_action_update')
				{
					$return_value = array_merge($return_value, array($this->access_rights));
				}
			}
			else
			{
				$return_value = array
					(
						'js_action_message_not_clear'
						, array($this->lang->line('hris_msg_parent_same_level_error'), 'error')
					);
			}


			$return_value = json_encode($return_value);
			echo $return_value;
		}
	}


	/**
	 * @function ajx_update()
	 * @description update the data
	 */
	public function ajx_update()
	{
		if ($this->input->is_ajax_request() && $this->access_rights['EDIT'])
		{
			/**
			 * @description load the models
			 */
			$this->load->model('admin/M_live_data');


			/**
			 * @description user choose logo to upload? add to master_media and get the attachment__id
			 */
			$logo_attachment_id = '';
			if (trim($this->input->post('txtinput[20]') != ''))
			{
				$file_data = array
					(
						'date' => date('Y-m-d H:i:s')
						, 'title' => $this->input->post('txtinput[4]').' Logo'
						, 'file_name' => $this->input->post('txtinput[20]')
						, 'file_mime' => $this->input->post('txtinput[21]')
						, 'slug' => 'company-logo-of-'.url_title($this->input->post('txtinput[4]'), 'dash', TRUE)
					);
				$this->M_live_data->insert
					(
						'db_default'
						, 'hris_master_media'
						, array('id' => NULL)
						, $file_data
						, 'form'
						, FALSE, ''
						, FALSE
					);

				$logo_attachment_id = $this->M_database->max_id('db_default', 'hris_master_media', 'id');
			}


			$company_profile_data = array
				(
					'parent_id' => $this->input->post('txtinput[1]')
					, 'code' => $this->input->post('txtinput[2]')
					, 'type' => $this->input->post('txtinput[3]')
					, 'name' => $this->input->post('txtinput[4]')
					, 'name_official' => $this->input->post('txtinput[5]')
					, 'company_level__id' => $this->input->post('txtinput[6]')
					, 'owner_name' => $this->input->post('txtinput[7]')
					, 'instantion_tax__id' => $this->input->post('txtinput[8]')
					, 'tax_number' => $this->input->post('txtinput[9]')
					, 'tax_ump_limit' => $this->input->post('txtinput[10]')
					, 'address' => $this->input->post('txtinput[11]')
					, 'country__id' => $this->input->post('txtinput[12]')
					, 'province__id' => $this->input->post('txtinput[13]')
					, 'city__id' => $this->input->post('txtinput[14]')
					, 'zipcode' => $this->input->post('txtinput[15]')
					, 'phone' => $this->input->post('txtinput[16]')
					, 'fax' => $this->input->post('txtinput[17]')
					, 'email' => $this->input->post('txtinput[18]')
				);
			$return_value = $this->M_live_data->update
				(
					'db_default'
					, $this->tbl_attr['TABLE']['NAME']
					, array('id' => $this->input->post('txtinput[0]'))
					, $logo_attachment_id != ''
						? array_merge
							(
								array('logo_attachment__id' => $logo_attachment_id)
								, $company_profile_data
							)
						: $company_profile_data
					, 'form', 'js_action_update', $this->tbl_attr['URL']['VIEW']
					, FALSE
				);


			$return_value = json_encode($return_value);
			echo $return_value;
		}
	}


	/**
	 * @function ajx_delete()
	 * @description delete the data
	 */
	public function ajx_delete($prm_key = '')
	{
		if ($this->input->is_ajax_request() && $this->access_rights['DELETE'])
		{
			/**
			 * @description load the models
			 */
			$this->load->model('admin/M_live_data');


			$return_value = $this->M_live_data->delete
				(
					'db_default'
					, $this->tbl_attr['TABLE']['NAME']
					, $prm_key
					, array($this->input->post('txtinput[0]'))
					, array('id')
				);

			/**
			 * @description when success, set the sub as root
			 */
			switch ($return_value[0])
			{
				case 'js_action_delete_list':
				case 'js_action_delete':
					$this->M_database->update('db_default', $this->tbl_attr['TABLE']['NAME'], array('parent_id' => 0), array('parent_id' => $prm_key), FALSE);
					break;
			}


			$return_value = json_encode($return_value);
			echo $return_value;
		}
	}


	/**
	 * @function ajx_data_put()
	 * @description get the data from database and put them to the form via ajax!
	 */
	public function ajx_data_put($prm_key = '')
	{
		if ($this->input->is_ajax_request() && $this->access_rights['EDIT'] && trim($prm_key) != '')
		{
			/**
			 * @description load the models
			 */
			$this->load->model('admin/M_live_data');


			$return_value = $this->M_live_data->put
				(
					'db_default'
					, $this->tbl_attr['TABLE']['NAME']
					, array('id' => $prm_key)
					, $this->tbl_attr['FORM']['FIELDS']
				);


			$return_value = json_encode($return_value);
			echo $return_value;
		}
	}



/**
 * @section AJAX Live Section
 */

	/**
	 * @function ajx_live_area_combo_put()
	 * @description get the data and put them in combobox
	 */
	public function ajx_live_area_combo_put($prm_parent_id = '0', $prm_area_type_id = '', $prm_object_start = '0', $prm_selected_value = '')
	{
		if ($this->input->is_ajax_request() && trim($prm_area_type_id) != '')
		{
			/**
			 * @description load the models
			 */
			$this->load->model('admin/hris/HRIS_M_live_data');


			$return_value = $this->HRIS_M_live_data->combo_fill('db_default', 'hris_master_area', $prm_object_start, 'NORMAL', array('parent_id' => $prm_parent_id, 'area_type__id' => $prm_area_type_id), TRUE, $prm_selected_value);


			$return_value = json_encode($return_value);
			echo $return_value;
		}
	}


	/**
	 * @function ajx_media_upload_file()
	 * @description upload the file
	 */
	public function ajx_media_upload_file()
	{
		/**
		 * @description load the settings
		 */
		$this->config->load(ENVIRONMENT.'/admin/hris_config', TRUE);
		$this->web_data['HRIS']['SETTINGS'] = $this->config->item(ENVIRONMENT.'/admin/hris_config');


		$upload_path = $this->web_data['HRIS']['SETTINGS']['HRIS_UPLOAD_DIR'].'/company/logo/';


		/**
		 * @description create the media folder if not exists
		 */
		if (is_dir($upload_path) == FALSE)
		{
			mkdir($upload_path, 0755);
		}
		$upload_path .= '/';

		/**
		 * @description upload configuration
		 */
		$upload_config = array
			(
				'upload_path' => $upload_path
				, 'allowed_types' => $this->web_data['HRIS']['SETTINGS']['HRIS_UPLOAD_TYPE_ALLOWED']
				, 'max_size' => $this->web_data['HRIS']['SETTINGS']['HRIS_UPLOAD_SIZE_MAX']
				, 'overwrite' => TRUE
			);
		$this->load->library('upload', $upload_config);
		$this->upload->initialize($upload_config);

		if ( ! $this->upload->do_upload('txtupload_file'))
		{
			$upload_result = $this->upload->data();

			/**
			 * @description let's allow the process without uploading the file :)
			 */
			if ($upload_result['file_size'] != 0)
			{
				$return_value = array
					(
						'js_upload_error'
						, array($this->upload->display_errors('', ''), 'error')
					);
			}
			else
			{
				$return_value = array
					(
						'js_upload_success'
						, array
							(
								'#txt20', ''
								, '#txt21', ''
							)
					);
			}
		}
		else
		{
			$upload_result = $this->upload->data();


			$return_value = array
				(
					'js_upload_success'
					, array
						(
							'#txt20', $upload_result['file_name']
							, '#txt21', $upload_result['file_type']
						)
				);
		}


		$return_value = json_encode($return_value);
		echo $return_value;
	}



}

/** End of file profile.php */
/** Location: ./biskitz_mpc/biskitz/controllers/admin/hris/configuration/organization/company/profile.php */
